package com.example.flutter_encryption;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.UUID;

public class Des3 {

    private final static String iv = "01234567";

    private final static String encoding = "utf-8";

    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };

    public static String generateShortUuid() {      //生成短id,8bit
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    /**
     *
     * 3DES加密
     *
     *
     *
     * @param plainText
     *            普通文本
     *
     * @return
     *
     * @throws Exception
     */

    public static String encode(String plainText,String secretKey) throws Exception {

        Key deskey = null;

        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());

        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");

        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");

        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);

        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
//        System.out.println(DesUtil.bytesToHexString(encryptData));
        return Base64.encodeBase64String(encryptData);

    }

    /**
     *
     * 3DES解密
     *
     *
     *
     * @param encryptText
     *            加密文本
     *
     * @return
     *
     * @throws Exception
     */

    public static String decode(String encryptText,String secretKey) throws Exception {
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        Key deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
        byte[] decryptData = cipher.doFinal(Base64.decodeBase64(encryptText));
        return new String(decryptData, encoding);
    }

    public static String decryptWithDESCEBZeroPading(String encryptMsg,String key) throws Exception{
            byte[] bytes = key.getBytes(encoding);
            SecretKey deskey = new SecretKeySpec(bytes, "DESede");
            Cipher instance = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            instance.init(Cipher.DECRYPT_MODE, deskey,new IvParameterSpec(iv.getBytes()));
            byte[] encryptMsgBytes = Base64.decodeBase64(encryptMsg);
            byte[] doFinal = instance.doFinal(encryptMsgBytes);
            return new String(doFinal, encoding);
    }

    public static void main(String[] args) throws Exception {
          System.out.println(decryptWithDESCEBZeroPading("qViZyrCxpgXSaYtFvNXb31fA3tRPXF0szOZzgeuXLrzdLRyrVMceum9k2HbqTDlHtZCfgN1L/CD3MRmq7re1/mQTYMvPzQL4iO+eB2fCNr/LYUuSJ5UvV/Uju2zJsE2l","ABB7A9726E68BB4729653C7C"));
//        System.out.println(MD5.encode(generateShortUuid()).substring(0,16));
    }

}

